#include "webpage.h"
#include "libs/controls/msgutils.h"
#include <QtDebug>
#include <QWebEngineCertificateError>

WebPage::WebPage(QObject* parent) : QWebEnginePage(parent), always_confirm_(false)
{
    // 处理权限申请
    connect(this, &QWebEnginePage::permissionRequested, this,
            [](QWebEnginePermission permissionRequest) {
                qDebug() << "Permission requested" << permissionRequest.permissionType();
                permissionRequest.grant();
            });
    // 处理渲染进程异常结束
    connect(
            this, &QWebEnginePage::renderProcessTerminated, this,
            [this](QWebEnginePage::RenderProcessTerminationStatus status, int exit_code) {
                if (status == QWebEnginePage::CrashedTerminationStatus
                    || status == QWebEnginePage::KilledTerminationStatus) {
                    qDebug() << "The page has been terminated" << status << exit_code;
                    this->triggerAction(QWebEnginePage::Reload);
                }
            },
            Qt::QueuedConnection);
}

WebPage::WebPage(QWebEngineProfile* profile, QObject* parent)
    : QWebEnginePage(profile, parent), always_confirm_(false)
{
    // 处理权限申请
    connect(this, &QWebEnginePage::permissionRequested, this,
            [](QWebEnginePermission permissionRequest) {
                qDebug() << "Permission requested" << permissionRequest.permissionType();
                permissionRequest.grant();
            });
    // 处理渲染进程异常结束
    connect(
            this, &QWebEnginePage::renderProcessTerminated, this,
            [this](QWebEnginePage::RenderProcessTerminationStatus status, int exit_code) {
                if (status == QWebEnginePage::CrashedTerminationStatus
                    || status == QWebEnginePage::KilledTerminationStatus) {
                    qDebug() << "The page has been terminated" << status << exit_code;
                    this->triggerAction(QWebEnginePage::Reload);
                }
            },
            Qt::QueuedConnection);
}

WebPage::~WebPage() { }

void WebPage::javaScriptAlert(const QUrl& securityOrigin, const QString& msg)
{
    if (allow_dialog_) {
        Q_EMIT aboutToShowDialog();
        yc::msgutils::msgBox(nullptr, tr("Alert"), msg, QMessageBox::Ok, QMessageBox::Ok);
        Q_EMIT closedDialog();
    }
    return;
}

bool WebPage::javaScriptConfirm(const QUrl& securityOrigin, const QString& msg)
{
    if (always_confirm_ || !allow_dialog_) {
        return true;
    } else {
        Q_EMIT aboutToShowDialog();
        auto ret = yc::msgutils::msgBox(nullptr, tr("Confirm"), msg,
                                        QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
        Q_EMIT closedDialog();
        if (ret == QMessageBox::Ok) {
            return true;
        }
        return false;
    }
}

bool WebPage::javaScriptPrompt(const QUrl& securityOrigin, const QString& msg,
                               const QString& defaultValue, QString* result)
{
    if (allow_dialog_) {
        return QWebEnginePage::javaScriptPrompt(securityOrigin, msg, defaultValue, result);
    } else {
        if (result) {
            *result = defaultValue;
        }
        return true;
    }
}

void WebPage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
                                       const QString& message, int lineNumber,
                                       const QString& sourceID)
{
    if (level == JavaScriptConsoleMessageLevel::InfoMessageLevel) {
        qInfo() << message << lineNumber << sourceID;
    } else if (level == JavaScriptConsoleMessageLevel::WarningMessageLevel) {
        // qWarning() << message << lineNumber << sourceID;
    } else if (level == JavaScriptConsoleMessageLevel::ErrorMessageLevel) {
        qCritical() << message << lineNumber << sourceID;
    } else {
        qDebug() << message << lineNumber << sourceID;
    }
}

bool WebPage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame)
{
    if (accept_navigation_function_) {
        return accept_navigation_function_(url, type, isMainFrame);
    }
    return true;
}
